#!/bin/python

# --------------------------------------
# VIDEO BUDDHA - nfo_tv
# --------------------------------------
# Ranames files based on a specific regex pattern in 
#  show_list.db and based on a specific list of file 
#  extensions.
# --------------------------------------

import os

import dep.thetvdbapi
import renamer

class NFO_TV(renamer.Renamer):
	
	def __init__(self):
		renamer.Renamer.__init__(self)
		self.tvdb = dep.thetvdbapi.TheTVDB('33E0195DC85C967E')

	def write_nfo(self, ep, filename, dir, seasep, rename_file):
		""" Write the actual .nfo file """
		# Prepend 0s if the number is low enough
		if int(ep.season_number) < 10:
			season_number = str('0' + ep.season_number)
		else:
			season_number = ep.season_number
			
		if int(ep.episode_number) < 10:
			episode_number = str('0' + ep.episode_number)
		else:
			episode_number = ep.episode_number
		
		print(ep.id + " - s" + season_number  + "e" + episode_number + " - " + ep.name)
		tvdb_seasep = "s" + season_number  + "e" + episode_number
		
		xml_src = """<episodedetails>
	<title>%s</title>
	<rating>%s</rating>
	<season>%s</season>
	<episode>%s</episode>
	<plot>%s</plot>
	<credits>%s</credits>
	<aired>%s</aired>					
<episodedetails>""" % (ep.name, ep.rating, ep.season_number, ep.episode_number, ep.overview, ep.writer, ep.first_aired)
			
		if tvdb_seasep == seasep:				
			print(dir, filename)
			print("Writing .nfo for: " + os.path.join(dir, rename_file))
			
			try:
				nfo = open(os.path.join(dir, os.path.splitext(rename_file)[0] + '.nfo'), 'w')
				nfo.write(xml_src)
				nfo.close()
			
				if self.logging: self.log("NFO File: " + rename_file)
				if self.console: print("NFO File: " + rename_file)	
			except:
				if self.logging: self.log("Couldn't write NFO File: " + rename_file)
				
	def find_shows(self, dir, old_file, rename_file):
		""" Find the show information and return any matching shows """
		if self.logging: self.log("Finding and writing NFO information:")
		if self.logging: self.log("\tDir: " + dir + " | Filename: " + rename_file)
		print dir, rename_file
		
		self.set_dir(dir)
		dir_title_and_file = self.get_title(dir, old_file)
		seasep_and_trail = self.get_seasep_trail(dir_title_and_file)

		dir = seasep_and_trail[0]
		filename = seasep_and_trail[1]
		title = seasep_and_trail[2]
		seasep = seasep_and_trail[3]
		trail = seasep_and_trail[4]
		
		matching_shows = self.tvdb.get_matching_shows(title)

		if self.logging: 
			self.log("Found shows matching given title: " + title)
			for show in matching_shows:
				self.log("\t" + show[1])
		
		return matching_shows
		
		#for (id, show) in matching_shows:
		#	if self.logging: self.log("Finding NFO information")
		#	if self.logging: self.log("\t" + id + " " + show)
		#	if self.logging: self.log("Matching show(s) found:")
		#	if self.logging: self.log("\t" + id + " " + show)
		#	
		#	show_and_eps = self.tvdb.get_show_and_episodes(id)
		#	eps = show_and_eps[1]
			
		#	self.write_nfo(eps, filename, dir, seasep, rename_file)